#include <iostream>
#include <map>
#include <string>

#include <stdio.h>
#include <stdlib.h>
#include <assert.h>
#include <string.h>
#include <unistd.h>

using namespace std; 

#define  N 32
#define  K 16

char num[N]; 
char rule[K][2]; 
int k; 

typedef long long ll; 

ll dfs(char *p)
{
	char c; 
	int found, tgt=0; 
	ll res = 1, ret; 
	if (*p=='\0') {
		return 1; 
	}
	tgt |= (1<<(*p-'0')); 
	do {
		found = 0; 
		for (int j=0; j<10; j++) { 
			if ((tgt&(1<<j))==0) {
				continue; 
			}
			c = j+'0'; 
			for (int i=0; i<k; i++) {
				if (rule[i][0]!=c)
					continue; 
				if (tgt & (1<<(rule[i][1]-'0')))
					continue; 
				found = 1; 
				tgt |= 1<<(rule[i][1]-'0'); 
				res ++; 
			}
		}
	} while(found); 
	ret = res*dfs(p+1); 

	return ret; 
}

int main()
{
	int o, t; 
	ll res; 
	//freopen("p1037_4.in", "r", stdin); 
	scanf("%s %d", num, &k); 
	for (int i=0; i<k; i++) {
		scanf("%d %d", &o, &t); 
		rule[i][0] = '0' + o; 
		rule[i][1] = '0' + t; 
	}
	res = dfs(num); 
	cout<<res<<"\n"; 
	return 0; 
}



